
// 获取应用实例
import {$request} from '../../utils/request'
const app = getApp()
Page({
  data: {
    num: 0,
    interval: "",
    isPlay: true,
    longitude: "123.28",
    latitude: "40.29",
    scale: 8.5,
    enable_traffic: true,
    show_compass: false,
    route: [],
    spots:[],
    markers: [
      {
        iconPath: "https://wechats.oss-cn-beijing.aliyuncs.com/begin.png",
        id: 1,
        longitude: "123.28",
        latitude: "40.29",
        height: 40,
        width: 40
      },
      {
        iconPath: "https://wechats.oss-cn-beijing.aliyuncs.com/location.png",
        id: 2,
        longitude: "123.03563",
        latitude: "39.905388",
        height: 20,
        width: 20,
        label: {
          anchorY:-50,
          content: "大连银石滩国家森林公园",
          padding: 5,
          fontSize: 12,
          textAlign: 'center',
          display: 'ALWAYS',
          borderRadius: 5,
          borderWidth: 1,
          bgColor: '#ffffff',
          borderColor: "#c3c3c3"
        },
      },
      {
        iconPath: "https://wechats.oss-cn-beijing.aliyuncs.com/location.png",
        id: 3,
        longitude: "122.978984",
        latitude: "40.00355",
        height: 20,
        width: 20,
        label: {
          anchorY:-50,
          content: "大连冰峪沟国家地质公园",
          padding: 5,
          fontSize: 12,
          textAlign: 'center',
          display: 'ALWAYS',
          borderRadius: 5,
          borderWidth: 1,
          bgColor: '#ffffff',
          borderColor: "#c3c3c3"
        },
      },
        {
          iconPath: "https://wechats.oss-cn-beijing.aliyuncs.com/location.png",
          id: 4,
          longitude: "123.03",
          latitude: "40.18",
          height: 20,
          width: 20,
          label: {
            anchorY:-50,
            content: "龙潭湾风景区",
            padding: 5,
            fontSize: 12,
            textAlign: 'center',
            display: 'ALWAYS',
            borderRadius: 5,
            borderWidth: 1,
            bgColor: '#ffffff',
            borderColor: "#c3c3c3"
          },
      },
      {
        iconPath: "https://wechats.oss-cn-beijing.aliyuncs.com/location.png",
        id: 5,
        longitude: "122.98645",
        latitude: "40.135608",
        height: 20,
        width: 20,
        label: {
          anchorY:-40,
          content: "大连天门山国家森林公园",
          padding: 5,
          fontSize: 12,
          textAlign: 'center',
          display: 'ALWAYS',
          borderRadius: 5,
          borderWidth: 1,
          bgColor: '#ffffff',
          borderColor: "#c3c3c3"
        },
      },
      {
        iconPath: "https://wechats.oss-cn-beijing.aliyuncs.com/end.png",
        id: 6,
        longitude: "122.97",
        latitude: "39.68",
        height: 40,
        width: 40
      }
    ],
    alongScenic:[]
  },
  onLoad(options) {
    var _this = this
    var pointfor = []
   
    //通过wx.request发起HTTPS接口请求
    wx.request({
      //地图WebserviceAPI驾车路线规划接口 请求路径及参数（具体使用方法请参考开发文档）
      url: 'https://restapi.amap.com/v3/direction/driving?key=21571ff7adb0223185347d34893558f3&origin=116.434446,39.90816&destination=116.481028,39.989643&extensions=base&strategy=0&waypoints=116.357483,39.907234',
      success(res) {
        var route = res.data.route;
        var paths = route.paths;
        var steps = paths[0].steps; //选择第一个路径导航
        var pl = [];
        //将解压后的坐标放入点串数组pl中
        for (var i = 0; i < steps.length; i++) {
          var polylines = steps[i].polyline.toString().split(";");
          for (var j = 0; j < polylines.length; j++) {
            pl.push({
              longitude: polylines[j].toString().split(",")[0],
              latitude: polylines[j].toString().split(",")[1]
            })
          }
        };
        for (var i = 0; i < pl.length; i++) {
          pointfor.push({
            longitude: pl[i].longitude,
            latitude: pl[i].latitude,
            id: i
          })
        }
        _this.setData({
          // 将路线的起点设置为地图中心点
          longitude: "122.978984",
          latitude: "40.00355",
          route: pl,
          // 绘制路线
          polyline: [{
            points: pl,
            color: '#58c16c',
            width: 6,
            borderColor: '#2f693c',
            borderWidth: 1
          }],
        })
      }
    });
    this.getalongScenic(options.id)
  },
 getalongScenic(id){
   $request({url:`/route/searchRouteByIdForMiniprogram?id=${id}`})
   .then((res)=>{
   this.setData({
     alongScenic:res.data.alongScenics,
     spots:res.data.alongScenics[0].spots
   })
   console.log(res.data.alongScenics)
   })
   .catch(catcherr => {
    console.error(catcherr);
  })
 },
  gotoPage:function () {
    wx.navigateTo({
      url: '../begin/begin', //要跳转到的页面路径
    })
  },
})